<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/stores'
import { useRouter, useRoute } from 'vue-router'
import { getorderCarApi } from '@/api/publish'
import { createOrderApi } from '@/api/order'
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()

const car = ref({})
const setCar = async () => {
  const res = await getorderCarApi(route.params.id)
  car.value = res.data
}

// 提交订单
const submit = async () => {
  //1. 创建订单，获取订单id
  const res = await createOrderApi({
    uid: userStore.userInfo.id,
    vid: route.params.id,
    orderPrice: car.value.orderPrice
  })
  const oid = res.data
  //2. 跳转到支付页
  router.replace(`/order/payPage/${oid}`)
}
onMounted(() => setCar())
</script>

<template>
  <div class="xtx-pay-checkout-page">
    <div class="container">
      <div class="wrapper">
        <!-- 商品信息 -->
        <h3 class="box-title">车辆信息</h3>
        <div class="box-body" style="padding-top: 10px">
          <table class="goods">
            <thead>
              <tr>
                <th width="320">商品信息</th>
                <th width="150">总价</th>
                <th width="320">交易地址</th>
                <th width="150">联系方式</th>
                <th width="320">车主备注</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <car-item-publish :car="car" disable size="order" />
                </td>
                <td>&yen;{{ car.currentPrice }}</td>
                <td>{{ car.tradeAddr || car.campusOrShopName }}</td>
                <td>{{ car.telNum }}</td>
                <td>{{ car.leaveMessage }}</td>
              </tr>
            </tbody>
          </table>
        </div>
        <!-- 注意事项 -->
        <h3 class="box-title">注意事项</h3>
        <ul style="margin: 0 0 20px 40px">
          <li><el-text type="success"> 1. 24h之前可免费退订</el-text></li>
          <li>
            <el-text type="info">
              2.
              24h之后72小时之前退订若上家不同意，将支付定金30%的违约金</el-text
            >
          </li>
          <li>
            <el-text type="info">
              3. 72小时之后退订需上家同意，否则将支付100%的违约金</el-text
            >
          </li>
          <li></li>
        </ul>
        <!-- 金额明细 -->
        <h3 class="box-title">金额明细</h3>
        <div class="box-body" style="padding-top: 0">
          <div class="total">
            <dl>
              <dt>订单总价：</dt>
              <dd>¥{{ car.orderPrice }}</dd>
            </dl>
            <dl>
              <dt>应付总额：</dt>
              <dd class="price">
                {{ car.orderPrice }}
              </dd>
            </dl>
          </div>
        </div>
        <!-- 提交订单 -->
        <div class="submit">
          <el-button type="primary" size="large" @click="submit"
            >提交订单</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.xtx-pay-checkout-page {
  margin-top: 20px;

  .wrapper {
    background: #fff;
    padding: 0 20px;

    .box-title {
      font-size: 16px;
      font-weight: normal;
      padding-left: 10px;
      line-height: 70px;
      border-bottom: 1px solid #f5f5f5;
    }

    .box-body {
      padding: 20px 0;
    }
  }
}

.goods {
  width: 100%;
  border-collapse: collapse;
  border-spacing: 0;

  img {
    height: 150px;
    max-width: 300px;
    object-fit: cover;
    border-radius: 10px;
  }

  tr {
    th {
      background: #f5f5f5;
      font-weight: normal;
    }

    td,
    th {
      text-align: center;
      padding: 20px;
      border-bottom: 1px solid #f5f5f5;

      &:first-child {
        border-left: 1px solid #f5f5f5;
      }

      &:last-child {
        border-right: 1px solid #f5f5f5;
      }
    }
  }
}

.my-btn {
  width: 228px;
  height: 50px;
  border: 1px solid #e4e4e4;
  text-align: center;
  line-height: 48px;
  margin-right: 25px;
  color: #666666;
  display: inline-block;

  &.active,
  &:hover {
    border-color: $xtxColor;
  }
}

.total {
  dl {
    display: flex;
    justify-content: flex-end;
    line-height: 50px;

    dt {
      i {
        display: inline-block;
        width: 2em;
      }
    }

    dd {
      width: 240px;
      text-align: right;
      padding-right: 70px;

      &.price {
        font-size: 20px;
        color: $priceColor;
      }
    }
  }
}

.submit {
  text-align: right;
  padding: 30px 60px 60px;
  border-top: 1px solid #f5f5f5;
}
</style>
